// pages/shuju/shuju.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isproduct: false,
    iskan: true,
    product_code: '',
    service_name: '',
    isis: false,
    P:0,
    S:0
  },
  huan: function (e) {
    this.setData({
      isproduct: !this.data.isproduct
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setData({
    //   product:app.globalData.product,
    //   service:app.globalData.service_items,
    //   sum1:app.globalData.product_sum,
    //   sum2:app.globalData.service_items_sum
    // })
    this.onchcek1()
    this.onchcek2()
    let tableName = 'member'
    // 应用查询对象
    let Product = new wx.BaaS.TableObject(tableName)


    // 不设置查询条件
    Product.find().then(res => {
      // success
      this.setData({
        member: res.data.objects
      })
      // this.Sons()
    }, err => {
      // err
    })
  },
  onchcek1() {
    let tableName = 'product'
    // 应用查询对象
    let Product = new wx.BaaS.TableObject(tableName)


    // 不设置查询条件
    Product.find().then(res => {
      // success
      this.setData({
        product: res.data.objects,
        sum1: res.data.objects.length
      })
      this.Pons()
    }, err => {
      // err
    })
  },
  /*
   * service
   */
  chakan: function (e) {
    console.log(e.currentTarget.dataset.postid)
    var postid = e.currentTarget.dataset.postid
    this.setData({
      service_name: this.data.service[postid].item_name,
      iskan: false,
    })
    var items = []
    for (var i = 0; i < this.data.Sqi_items.length; i++) {
      if (this.data.Sqi_items[i].product_code == this.data.service_name) {
        items.push(this.data.Sqi_items)
      }
    }
    if (items[0] == undefined) {
      this.setData({
        Sitem_: []
      })
    } else {
      this.setData({
        Sitem_: items[0]
      })
    }
    var item = []
    for (var i = 0; i < this.data.Ssan_items.length; i++) {
      if (this.data.Ssan_items[i].product_code == this.data.service_name) {
        item.push(this.data.Ssan_items)
      }
    }
    if (item[0] == undefined) {
      this.setData({
        Sitem__: []
      })
    } else {
      this.setData({
        Sitem__: item[0]
      })
    }

    console.log(this.data.Sitem_)
  },
  // prouct

  Pchakan: function (e) {
    console.log(e.currentTarget.dataset.postid)
    var postid = e.currentTarget.dataset.postid
    this.setData({
      product_code: this.data.product[postid].product_code,
      iskan: false,
    })
    var items = []
    for (var i = 0; i < this.data.Pqi_items.length; i++) {
      if (this.data.Pqi_items[i].product_code == this.data.product_code) {
        items.push(this.data.Pqi_items)
      }
    }
    if (items[0] == undefined) {
      this.setData({
        item_: []
      })
    } else {
      this.setData({
        item_: items[0]
      })
    }
    var item = []
    for (var i = 0; i < this.data.Psan_items.length; i++) {
      if (this.data.Psan_items[i].product_code == this.data.product_code) {
        item.push(this.data.Psan_items)
      }
    }
    if (item[0] == undefined) {
      this.setData({
        item__: []
      })
    } else {
      this.setData({
        item__: item[0]
      })
    }
    console.log(this.data.item__)
  },
  gan: function (e) {
    this.setData({
      isis: !this.data.isis
    })
  },
  onchcek2() {
    let tableName = 'service_items'
    // 应用查询对象
    let Product = new wx.BaaS.TableObject(tableName)


    // 不设置查询条件
    Product.find().then(res => {
      // success

      this.setData({
        service: res.data.objects,
        sum2: res.data.objects.length
      })
      this.Sons()
    }, err => {
      // err
    })
  },
  Confirm: function (e) {
    this.setData({
      iskan: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //product
  Pons() {
    var that = this
    var qi_items = [] //七天
    var san_items = [] //三十天
    var sj = []
    var id = []
    for (var i = 0; i < that.data.sum1; i++) {
      if (that.data.product[i].shuju != undefined) {
        id.push(that.data.product[i]._id)
        for (var j = 0; j < that.data.product[i].shuju.length; j++) {
          var sjj = []
          var shuju = that.data.product[i].shuju[j].split(',')
          console.log(shuju)
          var phone = that.jiexi(shuju[1])
          console.log(phone)
          if (timeFn(shuju[4]) <= 7) {
            var qi_item = {
              phone: phone,
              product_code: shuju[0],
              sum: shuju[2]
            }
            qi_items.push(qi_item)
          }
          if (timeFn(shuju[4]) <= 30) {
            var san_item = {
              phone: phone,
              product_code: shuju[0],
              sum: shuju[3]
            }
            san_items.push(san_item)
            sjj.push(that.data.product[i].shuju[j])
            // id.push(that.data.product[i]._id)
          }else{
            that.setData({
              P:1
            })
          }

        }
        console.log(qi_items)
        console.log(san_items)
        sj.push(sjj)
      }
    }
    console.log(id)
    console.log(sj)
    this.setData({
      Pqi_items: qi_items,
      Psan_items: san_items,
      Psj: sj,
      Pid: id
    })

    function timeFn(d1) { //d1作为一个变量传进来
      //如果时间格式是正确的，那下面这一步转化时间格式就可以不用了
      var dateBegin = new Date(d1.replace(/-/g, "/")); //将-转化为/，使用new Date
      var dateEnd = new Date(); //获取当前时间
      var dateDiff = dateEnd.getTime() - dateBegin.getTime(); //时间差的毫秒数
      var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数

      if (dayDiff >= 0) {
        return dayDiff
      }

    }
    if(that.data.P == 1){
      this.onP()
    }
    
  },
  //service
  Sons() {
    var that = this
    var qi_items = [] //七天
    var san_items = [] //三十天
    var sj = []
    var id = []       //全部产品的数据
    for (var i = 0; i < that.data.sum2; i++) {
      if (that.data.service[i].shuju != undefined) {
        id.push(that.data.service[i]._id)
        var sjj = []    //每个产品的数据
        for (var j = 0; j < that.data.service[i].shuju.length; j++) {
          var shuju = that.data.service[i].shuju[j].split(',')
          console.log(shuju)
          var phone = that.jiexi(shuju[1])
          console.log(phone)
          if (timeFn(shuju[4]) <= 7) {
            var qi_item = {
              phone: phone,
              product_code: shuju[0],
              sum: shuju[2]
            }
            qi_items.push(qi_item)
          }
          if (timeFn(shuju[4]) <= 30) {
            var san_item = {
              phone: phone,
              product_code: shuju[0],
              sum: shuju[3]
            }
            san_items.push(san_item)
            sjj.push(that.data.service[i].shuju[j])
            console.log(sjj)
          }else{
            that.setData({
              S:1
            })
          }


        }
        console.log(qi_items)
        console.log(san_items)
        sj.push(sjj)
      }
    }
    console.log(sj)
    console.log(id)

    this.setData({
      Sqi_items: qi_items,
      Ssan_items: san_items,
      Ssj: sj,
      Sid: id
    })

    function timeFn(d1) { //d1作为一个变量传进来
      //如果时间格式是正确的，那下面这一步转化时间格式就可以不用了
      var dateBegin = new Date(d1.replace(/-/g, "/")); //将-转化为/，使用new Date
      var dateEnd = new Date(); //获取当前时间
      var dateDiff = dateEnd.getTime() - dateBegin.getTime(); //时间差的毫秒数
      var dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)); //计算出相差天数

      if (dayDiff >= 0) {
        return dayDiff
      }

    }
    if(that.data.S == 1){
      this.onK()
    }
    
  },
  onK() {
    var that = this
    for (var i = 0; i < that.data.Sid.length; i++) {
      let tableName1 = 'service_items'
      let recordID1 = that.data.Sid[i] // 数据行 id
  
      let Product2 = new wx.BaaS.TableObject(tableName1)
      let product2 = Product2.getWithoutData(recordID1)
  
      product2.set('shuju', this.data.Ssj[i])
      product2.update().then(res => {
        // success
      }, err => {
        // err
      })
    }
    // var item = []
    

  },
  onP() {
    var that = this
    for (var i = 0; i < that.data.Pid.length; i++) {
      let tableName1 = 'product'
      let recordID1 = that.data.Pid[i] // 数据行 id
  
      let Product2 = new wx.BaaS.TableObject(tableName1)
      let product2 = Product2.getWithoutData(recordID1)
  
      product2.set('shuju', this.data.Psj[i])
      product2.update().then(res => {
        // success
      }, err => {
        // err
      })
    }
    // var item = []
    

  },
  jiexi(name) {
    //解析
    var k = name.split('')
    var phone = ''
    console.log(k)
    for (var j = 0; j < k.length; j++) {
      // console.log(222)
      if (k[j] == 'A' || k[j] == 'B' || k[j] == 'C' || k[j] == 'D' || k[j] == 'E' || k[j] == 'F') {
        phone = phone + "8"
        // console.log(phone)
      } else if (k[j] == 'G' || k[j] == 'H' || k[j] == 'I' || k[j] == 'J' || k[j] == 'K' || k[j] == 'L') {
        phone = phone + "0"
      } else if (k[j] == 'M' || k[j] == 'N' || k[j] == 'O' || k[j] == 'P' || k[j] == 'Q' || k[j] == 'R') {
        phone = phone + '1'
      } else if (k[j] == 'S' || k[j] == 'T' || k[j] == 'U' || k[j] == 'V' || k[j] == 'W' || k[j] == 'X') {
        phone = phone + '2'
      } else if (k[j] == 'Y' || k[j] == 'Z' || k[j] == 'k' || k[j] == '?' || k[j] == '/' || k[j] == '|') {
        phone = phone + '3'
      } else if (k[j] == '7' || k[j] == '0' || k[j] == '@' || k[j] == '!' || k[j] == '~' || k[j] == '@') {
        phone = phone + '4'
      } else if (k[j] == '$' || k[j] == '-' || k[j] == '=' || k[j] == '&' || k[j] == 'T' || k[j] == '#') {
        phone = phone + '6'
      } else if (k[j] >= '1' && k[j] <= '6') {
        phone = phone + '5'
      } else if (k[j] == '&' || k[j] == '9' || k[j] == '8' || k[j] == '%' || k[j] == '+' || k[j] == '?') {
        phone = phone + '7'
      } else if (k[j] >= 'a' && k[j] <= 'f') {
        phone = phone + '9'
      }
    }
    // console.log(phone)
    return phone
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // wx.BaaS.reportTemplateMsgAnalytics(options)
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})